package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.PageBean;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小帅蓝
 * @Date 2022/6/15 13:56
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping
    public R<Category> add(@RequestBody Category category) {
        return categoryService.add(category);
    }

    @GetMapping("/page")
    public R<PageBean> queryAll(Integer page, Integer pageSize) {
        PageBean pageBean = categoryService.queryAll(page, pageSize);
        if (pageBean != null) {
            return R.success(pageBean);
        } else {
            return R.error("分类信息分页查询失败！");
        }
    }

    @DeleteMapping
    public R<String> delete(String id) {
        return categoryService.delete(id);
    }

    @PutMapping
    public R<Category> update(@RequestBody Category category) {
        return categoryService.update(category);
    }

    @GetMapping("/list")
    public R<List<Category>> getList(Integer type) {
        List<Category> list = categoryService.categoryList(type);
        return R.success(list);
    }

}
